<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Ad;
use app\common\model\ArticleLike;
use app\common\model\Content;
use app\common\model\Like;
use app\common\model\Unlike;
use app\service\ContentService;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Special extends Common
{

    public function __construct()
    {
        parent::__construct();
        $ad = Ad::where('board_id',7)->select();
        View::assign("ad",$ad);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {


        $cid = input("get.cid");
        if($cid){
            $where["cate_id"] = $cid;
        }
        $where["status"] =1;
        $where["model_id"] =6; //专题


        if(!isset($where)){
            $where = 1;
        }

        $list = \app\common\model\Content::with("user")
            ->where($where)
            ->order("top DESC,recommend DESC,id DESC")
            ->paginate([
                'query'=> $this->params,
            ]);

        $temp = $list->items();
        foreach ($temp as $k => $v){
            //更新文章的展现量
            update_hits($v["id"]);
        }

        //echo Db::name("article")->getLastSql();die;
        View::assign("list",$list);
        return  view();
    }

    public function search()
    {
        $keywords = \think\facade\Request::get("keywords");
        //将用户搜索的关键词存入到tag表中
        $res = Db::name("tags")
            ->whereLike('name',"%{$keywords}%")
            ->find();
        if(!$res){
            $temp["name"] = $keywords;
            $temp["status"] = 0;
            $temp["ip"] = \request()->ip();
            $temp["hits"] = 0;
            $temp["create_time"] = time();
            if(session("index_user")){
                $user = session("index_user");
                $temp["uid"] = $user["id"];
            }else{
                $temp["uid"] = 0;
            }
            Db::name("tags")->insert($temp);
        }else{
            Db::name("tags")
                ->whereLike('name',"%{$keywords}%")
                ->inc("hits",1)
                ->update();
        }
        if($keywords){
            $keywords = trim($keywords);
            View::assign('keywords',$keywords);
        }
        $list = \app\common\model\Content::with("user")
            ->whereLike('title',"%{$keywords}%")
            ->order("id DESC")
            ->paginate([
                'query'=> $this->params,
            ]);
        $temp = $list->items();
        foreach ($temp as $k => $v){
            //更新文章的展现量
            update_hits($v["id"]);
        }

        //echo Db::name("article")->getLastSql();die;
        View::assign("list",$list);
        return  view('index');
    }



    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $res = Content::find($id);
        //更新文章的展现量
        update_hits($id,2);

        $list =\app\common\model\Content::with("user")
            ->where("pid","=",$id)
            ->order("id DESC")
            ->paginate();

        $temp = $list->items();
        foreach ($temp as $k => $v){
            //更新文章的展现量
            update_hits($v["id"]);
        }

        View::assign("list",$list);

        View::assign("res",$res);
        return  view();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function cache_clear()
    {
        Cache::clear();
    }

    //简历模板
    public function jianli()
    {
        View::assign("list",'');
        return  view();
    }

    public function test()
    {

        //$list = \app\common\model\Article::with(["content"])
        //    ->where("like",">",0)
        //    ->where("handle","=",0)
        //    ->limit(10)
        //    ->order("id DESC")
        //    ->select()->toArray();
        //p($list);die;
        //foreach ($list as $k => $v){
        //    if($v["like"] > $v["content"]["like"]){
        //        Content::where("id",$v["contentid"])
        //            ->update(["like"=>$v["like"]]);
        //
        //        p($v["content"]["title"]."更新成功");
        //    }
        //    \app\common\model\Article::where("id",$v["id"])
        //        ->update(["handle"=>1]);
        //}
        $list = Unlike::with("article")
            ->where("cid",0)
            ->limit(100)
            ->select()->toArray();

        foreach ($list as $k => $v){
            \app\common\model\Unlike::
                where("id",$v["id"])
                ->update(["cid"=>$v["article"]["contentid"]]);
        }
        p(count($list));


    }
    public function RandAbc($length = "") { // 返回随机字符串
        $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        return str_shuffle($str);
    }

    /*
     * $content 文章内容
     * $order 要获取哪张图片，ALL所有图片，0第一张图片
    */
    public  function getImgs($content,$order='ALL')
    {
        $pattern = "/<img .*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern,$content,$match);
        if(isset($match[1]) && !empty($match[1])){
            if($order==='ALL'){
                return $match[1];
            }
            if(is_numeric($order) && isset($match[1][$order])){
                return $match[1][$order];
            }
        }
        return '';
    }


    //今日热门
    public  function hot_today()
    {
        $hot = ContentService::hot_day();
        View::assign("list",$hot);
        return  view();
    }
}
